{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Cognitive Services - Analyze Text\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from pyspark.sql import SparkSession\n",
    "from synapse.ml.core.platform import running_on_synapse, find_secret\n",
    "\n",
    "# Bootstrap Spark Session\n",
    "spark = SparkSession.builder.getOrCreate()\n",
    "if running_on_synapse():\n",
    "    from notebookutils.visualization import display\n",
    "\n",
    "cognitive_key = find_secret(\"cognitive-api-key\")\n",
    "cognitive_location = \"eastus\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = spark.createDataFrame(\n",
    "    data=[\n",
    "        [\"en\", \"Hello Seattle\"],\n",
    "        [\"en\", \"There once was a dog who lived in London and thought she was a human\"],\n",
    "    ],\n",
    "    schema=[\"language\", \"text\"],\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "display(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from synapse.ml.cognitive import *\n",
    "\n",
    "text_analyze = (\n",
    "    TextAnalyze()\n",
    "    .setLocation(cognitive_location)\n",
    "    .setSubscriptionKey(cognitive_key)\n",
    "    .setTextCol(\"text\")\n",
    "    .setOutputCol(\"textAnalysis\")\n",
    "    .setErrorCol(\"error\")\n",
    "    .setLanguageCol(\"language\")\n",
    "    .setEntityRecognitionParams(\n",
    "        {\"model-version\": \"latest\"}\n",
    "    )  # Can pass parameters to each model individually\n",
    "    .setIncludePii(False)  # Users can manually exclude tasks to speed up analysis\n",
    "    .setIncludeEntityLinking(False)\n",
    "    .setIncludeSentimentAnalysis(False)\n",
    ")\n",
    "\n",
    "df_results = text_analyze.transform(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "display(df_results)"
   ]
  }
 ],
 "metadata": {
  "description": null,
  "kernelspec": {
   "display_name": "Synapse PySpark",
   "name": "synapse_pyspark"
  },
  "language_info": {
   "name": "python"
  },
  "save_output": true
 },
 "nbformat": 4,
 "nbformat_minor": 0
}